﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UCControlBase;

namespace UCBtnExt
{
    [DefaultEvent("BtnClick")]
    public partial class BtnExt : ControlBase
    {
        private Color _btnBackColor = Color.White;
        [Description("按钮背景色"),Category("自定义")]
        public Color BtnBackColor
        {
            get { return _btnBackColor; }
            set 
            { 
                _btnBackColor = value;
                this.BackColor = value;
            }
        }

        private Color _btnForeColor = Color.Black;
        [Description("按钮字体颜色"),Category("自定义")]
        public Color BtnForeColor
        {
            get { return _btnForeColor; }
            set
            {
                _btnForeColor = value;
                lbl.ForeColor = value;
            }
        }

        private Font _btnFont = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
        [Description("按钮字体"), Category("自定义")]
        public Font BtnFont
        {
            get { return _btnFont; }
            set
            {
                _btnFont = value;
                lbl.Font = value;
            }
        }

        private string _btnText = "button1";
        [Description("按钮文字"), Category("自定义")]
        public string BtnText
        {
            get { return _btnText; }
            set
            {
                _btnText = value;
                lbl.Text = value;
            }
        }

        [Description("按钮点击事件"), Category("自定义")]
        public event EventHandler BtnClick;

        public BtnExt()
        {
            InitializeComponent();
            CornerRadius = 5;
        }

        protected override void OnResize(EventArgs e)//将lbl居中放置，这样按钮字体就可以居中显示
        {
            base.OnResize(e);
            int x = (int)(0.5 * (this.Width - lbl.Width));
            int y = (int)(0.5 * (this.Height - lbl.Height));
            lbl.Location = new Point(x, y);
        }

        private void BtnExt_Click(object sender, EventArgs e)
        {
            if (BtnClick != null)
                BtnClick(this, e);
        }   

        //////根据边框的颜色改变动态显示按钮是否被点击
        private Color c;
        private bool b;
        private void BtnExt_MouseDown(object sender, MouseEventArgs e)
        {
            b = IsShowRect;
            IsShowRect = true;
            c = RectColor;
            RectColor = Color.Purple;            
        }

        private void BtnExt_MouseUp(object sender, MouseEventArgs e)
        {
            IsShowRect = b;
            RectColor = c;   
            Refresh();
        }

        private Color backcolor;
        private void BtnExt_MouseEnter(object sender, EventArgs e)
        {
            backcolor = BtnBackColor;
            BtnBackColor = Color.FromArgb(50, 173, 254, 254);
        }

        private void BtnExt_MouseLeave(object sender, EventArgs e)
        {
            BtnBackColor = backcolor;
        }     
    }
}
